<?php

namespace App\Jobs;

use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log;
use App\Services\Com\MessageService;
use App\Models\Member\Member;

// php artisan make:job FeedsExposure
// php artisan make:job feedDispatch
// php artisan queue:work --queue=FeedDispatch,NotifyUsers
// php artisan queue:work --queue=NotifyUsers
// @人提示
class NotifyUsers implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;

    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 5;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data = $data;
    }

    /**
     * Execute the job.
     * $data:
     * array(2) {
          [0]=>
          array(3) {
            ["form_member_uuid"]=>
            string(36) "c1d83f4e-8d61-4af4-93e0-bbd4248c5b92"
            ["member_uuid"]=>
            string(36) "c1d83f4e-8d61-4af4-93e0-bbd4248c5b92"
            ["post_uuid"]=>
            int(1)
          }
          [1]=>
          array(3) {
            ["form_member_uuid"]=>
            string(36) "c1d83f4e-8d61-4af4-93e0-bbd4248c5b92"
            ["member_uuid"]=>
            string(36) "c1d83f4e-8d61-4af4-93e0-bbd4248c5b92"
            ["post_uuid"]=>
            int(1)
          }
        }
     * @return void
     */
    public function handle()
    {
        //处理用户推送
        try {
            $form_member_uuid = '';
            foreach ($this->data as $key => $value) {
                $to_member_uuids[] = $value['member_uuid'];
                $form_member_uuid = $value['form_member_uuid'];
            }
            if(empty($to_member_uuids) || empty($form_member_uuid)){
                Log::error('空用户');
                return;
            }
            $param['owner_uuid'] = $form_member_uuid;
            $param['title'] = '';
            $param['context'] = '';
            $param['type'] = 2;
            // $param['place'] = '';
            $param['route_link'] = '/app/FeedInfoActivity?params_data='.$value['post_uuid'];
            $param['members_uuid'] = $value['member_uuid'];//接收的用户
            
            //form 用户信息
            $membersInfo = Member::query()->where('uuid', $form_member_uuid)->first('nickname');
            
            $ret = MessageService::sendMessage($param, $to_member_uuids, $membersInfo);
            
        } catch (\Exception $exception) {
             $this->fail($exception);
        }
    }

    /**
     * 
     *  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'migo.laravel_failed_jobs' doesn't exist (SQL: insert into `laravel_failed_jobs` (`connection`, `queue`, `payload`, `exception`, `failed_at`) values (redis,
     **/
    public function fail($exception)
    {
        //
        Log::alert($exception->getMessage());   
    }
}


